<?php
class BargainAction extends CommonAction {
	//砍价列表
	public function index(){
		parent::_list();
		$list = $this->get('list');
		$pdb = M('Product');
		foreach($list as $k => $v){
			$product = $pdb -> where(array('site_id' => $this->siteid, 'id' => $v['product_id'])) -> find();
			$list[$k]['img_url'] = $product['img_url'];
			$list[$k]['reprice'] = $product['price'];
			
		}
		$this -> assign('list', $list);
		$this -> display();
	}
	
	
	public function edit(){
		$relatelist = array();
		$pid = $this->dao -> where(array('id' => $this->_get('id', 'int'))) -> getField('product_id');
		$relatelist[] = M('Product') -> where(array('id' => array('in', $pid))) -> find();
		$this->assign('relatelist', $relatelist);
		parent::edit();
	}
	
	
	public function update(){
		$_POST['product_id'] = $this->_post('relateids');
		parent::update();
	}

	function selectProduct(){
		$this->dao = D('Product');
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		$relateids = $this->dao -> where(array('id' => $this->_get('product_id'), 'site_id' => $this->siteid)) -> getField('relateids');
		$this->selectedid = explode(',', $relateids);
		//dump($selectedid);
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		$this->display();
	}
	
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$this->list = $this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name, c.name as cate_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->pages=$p->show();
	}
	
	public function add(){
		
		//供货商
		$this->SupplierList = M('Supplier') -> where(array('site_id' => $this->siteid)) -> select();
		$this->categorylist = D('Product_category') -> getListToArray1(0,$this->siteid);//dump($this->categorylist);
		$this->cate_list=D('Product_category')->getListToArray1(0,$this->siteid);
		//固定栏目及字段扩展
		$SiteModel=M('Site');
		$curTemplate=$SiteModel->where('id='.$this->siteid)->Field("productExtendsField as productExtendsField")->find();
		$this->extendsField=json_decode($curTemplate['productExtendsField'],true);
		parent::add();
	}
	
	
	//砍价订单
	public function order(){
		$bid = $this->_get('bid');
		$db = M('Bargain_order');
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$map['bo.bargain_id'] = $bid;
		$map['bo.order_id'] = array('gt', 0);
		$map['bo.site_id'] = $this->siteid;
		$count = $db -> alias('bo') -> where($map) -> count();
		$p=new Page($count,20);
		$list = $db->alias('bo')->Field('bo.*')->where($map)->order('bo.id')->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->listmap = 1;
		$ids = array();
		$ids[] = 0;
		foreach($list as $k => $v){
			$ids[] = $v['order_id'];
		}
		$this->dao = D('Order');
		$this->listmap .= " and o.id in(".implode(',', $ids).")";
		
        $this->listmap .= " and o.site_id=" . $this->siteid;

        if ($this->_get('keywords')) {
            $this->listmap .= " and (yourname like '%".$this->_get('keywords')."%' or ordernumber like '%" . $this->_get('keywords') . "%' or member_id = '".$this->_get('keywords')."')";
            $this->keywords = $this->_get('keywords');
        }
        if ($this->_get('status', 'int') || $this->_get('status') == '0') {
            $this->listmap .= " and (o.status = " . $this->_get('status', 'int') . ")";
            $this->status = $this->_get('status');
        }
        if ($this->_get('stime')) {
            $this->listmap .= " and (create_time >= " . strtotime($this->_get('stime')) . ")";
            $this->stime = $this->_get('stime');
        }
        if ($this->_get('etime')) {
            $this->listmap .= " and (create_time <= " . (24 * 3600 + 1 * strtotime($this->_get('etime'))) . ")";
            $this->etime = $this->_get('etime');
        }
        parent::_list("o", "left join __MEMBER__ as m on m.id=o.member_id", "o.*,m.nickname,m.username");

        $list = $this->get('list');
        $saleDB = M('sale');

        foreach ($list as $key => $value) {
            $data = $saleDB->where(array('order_id' => $value['id']))->group("status")->select();
            $num = count($data);
            if ($num > 1) {

            } elseif ($num == 1 && $data['0']['status'] == 1) {

            }

            //会员ID


        }

        
		
		
		
		
        $this->pages=$p->show();
		$this->display();
	}
	
}

